package com.earl.eatbao.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.earl.eatbao.commons.dao.BaseRepsitoty;
import com.earl.eatbao.commons.service.BaseServiceImpl;
import com.earl.eatbao.dao.eatbao.ShopDao;
import com.earl.eatbao.domain.eatbao.Shop;
import com.earl.eatbao.exception.DomainSecurityException;
import com.earl.eatbao.service.ShopService;
import com.earl.eatbao.util.UpLoadFile;

@Service("shopService")
public class ShopServiceImpl extends BaseServiceImpl<Shop, Long> implements ShopService {

	// desc:日记工具logger
	private static final Logger logger = LoggerFactory.getLogger(ShopServiceImpl.class);

	@Resource
	private ShopDao shopDao;

	@Override
	protected BaseRepsitoty<Shop, Long> getDao() {
		return shopDao;
	}

	@Override
	public void shopOnline(Long userId) {
		logger.info("<====== 进入shopOnline:方法,{[userId] :" + userId + " } ====");
		chageShopOnilne(userId, 2);
		logger.info("==== 退出shopOnline方法 返回值类型:{ void: } =======>");
	}

	@Override
	public void shopOffline(Long userId) {
		logger.info("<====== 进入shopOffline:方法,{[userId] :" + userId + " } ====");
		chageShopOnilne(userId, 1);
		logger.info("==== 退出shopOffline方法 返回值类型:{ void: } =======>");
	}

	@Override
	public List<Shop> searchShop(String shopName) {
		//TODO 模糊搜索
		return null;
	}

	@Override
	public Boolean updateShop(Shop shop) {
		logger.info("<==== 进入updateShop:方法,{[shop] : } ====");
		Boolean result = false;
		if (null != shop ) {
			shopDao.save(shop);
			result = true;
		}
		logger.info("===== 退出updateShop方法 返回值类型:{ Boolean:" + result + " } ====>");
		return result;
	}

	@Override
	public Boolean upLoadShopImage(MultipartFile file, Long shopId) {

		logger.info("<====== 进入upLoadShopImage:方法,{[file, shopId] :" + shopId + " } ====");
		Boolean result = false;
		Shop shop = shopDao.findOne(shopId);
		if (null != shop) {
		    String imagePath = UpLoadFile.saveFile(file, UpLoadFile.getUUID());
		    if (null != imagePath) {
                shop.setShopImg(imagePath);
                shopDao.save(shop);
                result = true;
            } else {
		        logger.info("==== 保存图片失败 ====");
            }
		} else {
		    logger.info("==== 商店不存在 ====");
        }
        logger.info("==== 退出upLoadShopImage方法 返回值类型:{ java.lang.Boolean: " + result + " } =======>");
		return result;
	}

    @Override
    public List<Shop> findBusinessShop(long businessId) {
        logger.info("<==== 进入findBusinessShop:方法,{[businessId] :" + businessId + " } ====");
        logger.info("===== 退出findBusinessShop方法 返回值类型:{ List<Shop>: } =======>");
        return shopDao.findByBusinessId(businessId);
    }

    @Override
    public Shop addShop(Shop shop, MultipartFile file) {
        logger.info("<====== 进入addShop:方法,{[shop, file] : } ====");
        if (null != shop &&
				shop.getDistributionFee() >= 0 &&
				shop.getStartingPrice() >= 0) {
            shop.setDelete(false);
            shop.setCreatedDate(new Date());
            shop.setLastModifiedDate(new Date());
            shop.setCreatedBy(shop.getBusinessId() + "");
            shop.setShopImg(UpLoadFile.saveFile(file, UpLoadFile.getUUID()));
			logger.info("==== 退出addShop方法 返回值类型:{ Shop: 正常 } =======>");
			return shopDao.save(shop);
		} else  {
        	new DomainSecurityException("ERROR: 商店为空");
			logger.info("==== ERROR: 商店为空 ====");
		}
		logger.info("==== 退出addShop方法 返回值类型:{ Shop: null } =======>");
        return null;
    }

	/**
	 * 改变商店的在线状态
	 * @param userId  商家ID
	 * @param status  商店状态
	 */
	private void chageShopOnilne(Long userId, int status) {
		Shop shop = shopDao.findUniqueByBusinessId(userId);
		if (null != shop) {
			shop.setStatus(status);
			shopDao.save(shop);
		} else {
			logger.info(" === 该用户不存在商店 ===");
		}
	}

	@Override
	public List<Shop> findOnlineShop() {
		// TODO Auto-generated method stub
		
		return shopDao.findByStatus(1);
	}
}